<?php

require_once('functions/pageLoad.php');

$page_title = 'Contact us';

require_once('functions/recaptchalib.php');
$publickey = "6LdLhAYAAAAAABwx-1kiacx5VqTj5YWFnanQE3NS";
$privatekey = "6LdLhAYAAAAAABdiW_7-udhTrdGURvR6XtM9eUTe";

if($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['form'] == 'contact-us'){

$resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);

$fail = '';

$email_regex = "/^[^@]+@([-\w]+\.)+[A-Za-z]{2,4}$/i";

$fail.= (strlen($_POST['fullname']) == 0) ? '<li>Name was not entered</li>' : '';
$fail.= (strlen($_POST['telephone']) == 0) ? '<li>Telephone number not entered</li>' : '';
$fail.= (strlen($_POST['telephone']) > 0 && !ctype_digit(str_replace(" ", "", $_POST['telephone']))) ? '<li>Invalid telephone number entered, please use numbers only</li>' : '';
$fail.= (preg_match($email_regex, $_POST['email_address']) == 0) ? '<li>Invalid email address entered</li>' : '';
$fail.= (strlen($_POST['fullname']) < 10) ? '<li>Message was not entered</li>' : '';
$fail.= (!$resp->is_valid) ? '<li>Incorrect ReCaptcha phrase entered</li>' : '';

if(strlen($fail) > 0){

// PHAIL

$results = '<p style="color: #color: #cc0000; font-weight: bold;">Please correct the following errors;</p><ul style="color: #color: #cc0000; font-weight: bold;">'.$fail.'</ul>';

}

else
{

// FINE - SEND EMAIL

	require("classes/class.phpmailer.php");
	$mail = new PHPMailer();
	$mail->IsSMTP(); // telling the class to use SMTP
	$mail->Host = "localhost"; // SMTP server
	$mail->From = $settings['no_reply_email'];
	$mail->FromName = "I Want A Tradesman";

	$mail->AddAddress($settings['site_email']);

	$mail->Subject = "Contact form submission - I Want A Tradesman";
	$mail->Body = "Name: ".$_POST['fullname']."\r\nTelephone: ".$_POST['telephone']."\r\nEmail: ".$_POST['email_address']."\r\nMessage: ".$_POST['message'];

	$mail->Send();

	$results = '<p style="color: #009900; font-weight: bold;">Thank you, your message was sent successfully. We will get back to you shortly.</p>';

	$_POST['fullname'] = '';
	$_POST['telephone'] = '';
	$_POST['email_address'] = '';
	$_POST['message'] = '';

}

}

include('includes/meta.php');
include('includes/header.php');
include('includes/navigation.php');

?>

<h1>Contact us</h1>

<?php echo $results; ?>

<div id="error_div" style="display: none;"></div>

<p>To contact us, please complete the form below. We aim to respond to all queries within 24 hours.</p>

<p><strong>I Want A Tradesman</strong><br />Dalton House,<br />60 Windsor Avenue,<br />London<br />SW19 2RR<br /><br />Fax: 0203 318 3047</p>

<form method="post" action="/contact-us" id="contact-us">

<input type="hidden" id="form" name="form" value="contact-us" style="display: none;" />

<div id="registration_form">

<div id="name_wrap">

<div class="rowWrapper" id="fullname_wrap">
<div class="rowLeft"><label for="fullname">Name:</label></div>
<div class="rowRight"><input type="text" name="fullname" id="fullname" class="textbox" value="<?php echo $_POST['fullname']; ?>" /></div>
</div>

</div>

<div id="number_wrap">

<div class="rowWrapper" id="telephone_wrap">
<div class="rowLeft"><label for="telephone">Telephone:</label></div>
<div class="rowRight"><input type="text" name="telephone" id="telephone" class="textbox" value="<?php echo $_POST['telephone']; ?>" /></div>
</div>

</div>

<div id="email_wrap">

<div class="rowWrapper" id="email_address_wrap">
<div class="rowLeft"><label for="email_address">Email address:</label></div>
<div class="rowRight"><input type="text" name="email_address" id="email_address" class="textbox" value="<?php echo $_POST['email_address']; ?>" /></div>
</div>

</div>

<div class="rowWrapper" id="message_wrap">
<div class="rowLeft"><label for="message">Message:</label></div>
<div class="rowRight"><textarea name="message" id="message" class="textbox" rows="5" cols="30"><?php echo stripslashes($_POST['message']); ?></textarea></div>
</div>

<div class="rowWrapper">
<div class="rowLeft">&nbsp;</div>
<div class="rowRight" style="width: 450px; font-size: 16px;"><?php echo recaptcha_get_html($publickey); ?></div>
</div>

<div class="rowWrapper">
<div class="rowLeft">&nbsp;</div>
<div class="rowRight">
<script type="text/javascript">document.write('<'+'a onclick="return formval(\'contact-us\');" style="cursor: pointer;"><'+'img src="/images/submit_button.gif" alt="Submit" /><'+'/a>');</script>
<noscript><input type="submit" value="Submit" class="blue_submit_button" /></noscript></div>
</div>

</div>

</form>

<div style="display: none;"><img src="images/tick.png" alt="" /><img src="images/cross.png" alt="" /></div>

<?php 

include('includes/rightColumn.php');
include('includes/footer.php');

?>